package com.magic.gre.db.search;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.magic.gre.GREApp;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2019-01-14 17:28
 * 文件名: DemoDBManager
 * 描述:
 */
public class DemoDBManager {
    private static final String TAG = "DemoDBManager";
    private static DemoDBManager dbMgr = new DemoDBManager();
    private DbOpenHelper dbHelper;

    private DemoDBManager() {
        dbHelper = DbOpenHelper.getInstance(GREApp.getInstance().getApplicationContext());
    }

    public static synchronized DemoDBManager getInstance() {
        if (dbMgr == null) {
            dbMgr = new DemoDBManager();
        }
        return dbMgr;
    }

    /**
     * 批量存储数据
     *
     * @param userList
     */
    public synchronized void saveContacList(List<String> userList) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db.isOpen()) {
            for (String user : userList) {
                db.delete(SearchDao.TABLE_NAME, SearchDao.COLUMN_NAME_CONTENT + "=?", new String[]{user});
                ContentValues values = new ContentValues();
                values.put(SearchDao.COLUMN_NAME_CONTENT, user);
                db.replace(SearchDao.TABLE_NAME, null, values);
            }
        }
    }

    /**
     * 获取表里面所有的数据
     *
     * @return
     */
    public synchronized List<String> getContactMap() {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        List<String> list = new ArrayList<>();
        if (db.isOpen()) {
            //根据_id倒序查询
            Cursor cursor = db.rawQuery("select * from " + SearchDao.TABLE_NAME + " order by _id desc", null);
            while (cursor.moveToNext()) {
                String content = cursor.getString(cursor.getColumnIndex(SearchDao.COLUMN_NAME_CONTENT));
                list.add(content);
            }
            cursor.close();
        }
        return list;
    }

    /**
     * 删除单个数据
     *
     * @param content
     */
    public synchronized void deleteContact(String content) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        if (db.isOpen()) {
            db.delete(SearchDao.TABLE_NAME, SearchDao.COLUMN_NAME_CONTENT + " = ?", new String[]{content});
        }
    }

    /**
     * 保存单个数据
     *
     * @param content
     */
    public synchronized void saveContact(String content) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(SearchDao.COLUMN_NAME_CONTENT, content);

        if (db.isOpen()) {
            db.replace(SearchDao.TABLE_NAME, null, values);
        }
    }

    /**
     * 更新单个数据
     *
     * @param content
     */
    public synchronized void updataUser(String content) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db.isOpen()) {
            ContentValues values = new ContentValues();
            values.put(SearchDao.COLUMN_NAME_CONTENT, content);
            db.update(SearchDao.TABLE_NAME, values, SearchDao.COLUMN_NAME_CONTENT + "=?", new String[]{content});
        }
    }

    /**
     * 清空表
     */
    public synchronized void clearSQL() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db.isOpen()) {
            db.delete(SearchDao.TABLE_NAME, null, null);
        }
    }

    /**
     * 关闭数据库
     */
    public synchronized void closeDb() {
        if (dbHelper != null) {
            dbHelper.closeDB();
        }
        dbMgr = null;
    }
}
